﻿using System;
using System.Net;
using System.Net.Sockets;

namespace Hsenl.Network {
    public class UdpSocket {
        private IUdpReceiver _receiver;
        private IUdpSender _sender;

        public IUdpReceiver Receiver => this._receiver;
        public IUdpSender Sender => this._sender;

        public void AddReceiver(IUdpReceiver receiver) {
            this._receiver = receiver;
        }

        public void AddSender(IUdpSender sender) {
            this._sender = sender;
        }

        public bool ReceiveFromAsync() {
            if (this._receiver == null)
                return false;

            return this._receiver.ReceiveFromAsync();
        }

        public bool SendToAsync() {
            if (this._sender == null)
                return false;

            return this._sender.SendToAsync();
        }

        public void Close(bool closeSocket) {
            this._receiver?.Close(closeSocket);
            this._sender?.Close(closeSocket);
            this._receiver = null;
            this._sender = null;
        }

        public void Dispose() {
            this._receiver?.Dispose();
            this._sender?.Dispose();
            this._receiver = null;
            this._sender = null;
        }
    }
}